/*
 * Copyright 2013 David Schreiber
 *           2013 John Paul Nalog
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.funny.learning.english.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.androidquery.callback.ImageOptions;
import com.funny.learning.english.R;
import com.funny.learning.english.cover_flow.FancyCoverFlow;
import com.funny.learning.english.cover_flow.FancyCoverFlowAdapter;
import com.funny.learning.english.model.GameTypeEntity;

public class GameAdapter extends FancyCoverFlowAdapter {

    // =============================================================================
    // Private members
    // =============================================================================
    // =============================================================================
    // Supertype overrides
    // =============================================================================
	private ArrayList<GameTypeEntity> arrData;
	private LayoutInflater inflater;
	private Context context;
	private int pos;
	private AQuery mAQuery;
	
	
	public GameAdapter(Context context, ArrayList<GameTypeEntity> arrData, int pos) {
		this.arrData = arrData;
		inflater = LayoutInflater.from(context);
		this.context = context;
		this.pos = pos;
		
		mAQuery = new AQuery(context);
	}
	
    @Override
    public int getCount() {
        return arrData.size();
    }

    @Override
    public GameTypeEntity getItem(int i) {
        return arrData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getCoverFlowItem(int i, View reuseableView, ViewGroup viewGroup) {
    	if(reuseableView == null){
    		reuseableView = inflater.inflate(R.layout.flow_item,viewGroup, false);
    	}
    	LinearLayout rlIndex = (LinearLayout)reuseableView.findViewById(R.id.rlIndex);
    	ImageView ivTopic = (ImageView)reuseableView.findViewById(R.id.ivTopic);
    	TextView tvTopic = (TextView)reuseableView.findViewById(R.id.tvTopic);
    	
    	int heightLayout = context.getResources().getDimensionPixelSize(R.dimen.home_layout_height);
    	int widthLayout = context.getResources().getDimensionPixelSize(R.dimen.home_layout_width);
// 		int widthImage = (int) (heightImage);
 		
 		
    	rlIndex.setLayoutParams(new FancyCoverFlow.LayoutParams(widthLayout,heightLayout));
    	
    	AQuery aQuery = mAQuery.recycle(reuseableView);
		
		ImageOptions imageOptions = new ImageOptions();
		imageOptions.fileCache = true;
		imageOptions.memCache = true;
		imageOptions.fallback = R.drawable.ic_launcher;
    	
    	GameTypeEntity gameTypeEntity = arrData.get(i);
    	if(gameTypeEntity != null){
//    		ivTopic.setImageResource(homeEntity.getSrcImg());
    		tvTopic.setText(gameTypeEntity.getName());
    		
    		aQuery.id(ivTopic).image(gameTypeEntity.getUrl(), imageOptions);	
    	}
    	
    	if(pos != i){
    		rlIndex.setBackgroundResource(R.drawable.stroke_img_topic);
    	}else{
    		rlIndex.setBackgroundResource(R.drawable.stroke_img_topic_selected);
    	}
    	
//        ImageView imageView = null;
//
//        if (reuseableView != null) {
//            imageView = (ImageView) reuseableView;
//        } else {
//            imageView = new ImageView(viewGroup.getContext());
//            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
//            
//            int heightImage = context.getResources().getDimensionPixelSize(R.dimen.height);
//    		int widthImage = (int) (heightImage * 1.2);
//            
//            imageView.setLayoutParams(new FancyCoverFlow.LayoutParams(widthImage,heightImage));
//        }
//        imageView.setImageResource(this.getItem(i));
        return reuseableView;
    }
    
    public void setPos(int pos){
    	this.pos = pos;
    	notifyDataSetChanged();
    }
}
